package com.juyu.miliao.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonView;
import com.juyu.miliao.api.domain.BillDetailEnergy;
import com.juyu.miliao.api.domain.EnergyLevel;
import com.juyu.miliao.api.service.EnergyLevelService;
import com.juyu.miliao.api.service.UserInfoService;
import com.juyu.miliao.api.util.JacksonView;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.IllegalParamException;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/energy")
public class UserEnergyLevelController {
    @Resource
    private EnergyLevelService energyLevelService;
    @Resource
    UserInfoService userInfoService;

    /**
     * 获取等级列表
     * @return
     */
    @GetMapping()
    @JsonView(JacksonView.EnergyList.class)
    public List<EnergyLevel> getEnergyList(){
        return energyLevelService.getEnergyList();
    }

    /**
     * 车辆列表
     */
    @GetMapping("/vehicle/list")
    public JSONArray getVehicleList(Authentication token){
        Subject subject = (Subject) token.getPrincipal();
        return energyLevelService.getVehicleList(subject.getId());
    }

    /**
     * 获取用户等级信息
     * @param token
     * @param userId
     * @return
     */
    @GetMapping("/level/{userId}")
    public JSONObject getUserLevel(Authentication token, @PathVariable("userId") long userId){
        Subject subject = (Subject) token.getPrincipal();
        if(subject.getId() != userId){
            throw new IllegalParamException("用户只能查询自己等级");
        }
        return userInfoService.getUserLevel(userId);
    }

    /**
     * 获取用户能量明细信息
     * @param token
     * @param userId
     * @return
     */
    @GetMapping("/detail/{userId}/{page}-{pageSize}")
    public List<BillDetailEnergy> getBillDetailEnergy(Authentication token, @PathVariable("userId") long userId,
                                                      @PathVariable("page") Integer page,
                                                      @PathVariable("pageSize") Integer pageSize){
        Subject subject = (Subject) token.getPrincipal();
        if(subject.getId() != userId){
            throw new IllegalParamException("用户只能查询自己等级");
        }
        if (page <= 0) {
            page = 1;
        }
        if (pageSize <= 0) {
            pageSize = 20;
        }
        return energyLevelService.getUserBillDetailEnergy(userId,page,pageSize);
    }

    /**
     * 根据levelId查询用户还需多少能量能够到达该等级
     * @param token
     * @param levelId
     * @return
     */
    @GetMapping("/level/difference/{levelId}")
    public JSONObject getLevelDifference(Authentication token,@PathVariable("levelId") Integer levelId){
        Subject subject = (Subject) token.getPrincipal();
        return energyLevelService.getLevelDifference(subject.getId(),levelId);
    }
}
